package com.mcy.springbatch.writer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mcy.springbatch.entity.SysUser;
import net.sf.json.JSONObject;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 利用FlatFileItemWriter把json写到文件
 * @author : Create By mcy
 * @date : 2019/8/25 15:55:20
 */
@Component
public class SysUserFlatFileItemWriter extends FlatFileItemWriter<SysUser> {

    @PostConstruct
    public void init(){
        setResource(new FileSystemResource("/Users/calon/Desktop/userdata.json"));
        ObjectMapper mapper = new ObjectMapper();
        setLineAggregator(item -> JSONObject.fromObject(item).toString());
    }
}
